package reflect.info;

import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Type;

/**
 * <p>
 * info demo
 * </P>
 *
 * @author dingwen
 * @date 2021.10.12 10:36
 */
public class InfoDemo {
    public static void main(String[] args) throws Exception{
        classInfo();
    }


    public static  void classInfo() throws Exception {
        Class<?> developerClass = Class.forName("reflect.info.Developer");
        Object o = developerClass.newInstance();
        String fullClassName = developerClass.getName();
        String simpleName = developerClass.getSimpleName();


        Field isLeader = developerClass.getDeclaredField("isLeader");
        // 静态属性获取值
        System.out.println("isLeader.get(null) = " + isLeader.get(null));
        // 获取所有public修饰的属性，本类以及父类的
        // 默认修饰符 是 0 ， public 是 1 ，private 是 2 ，protected 是 4 , static 是 8 ，final 是 16
        Field[] fields = developerClass.getFields();
        for (Field field : fields) {
            System.out.println("field.getName() = " + field.getName());
            System.out.println("field.getModifiers() = " + field.getModifiers());
            System.out.println("field = " + field);
        }

        // 获取本类中所有的属性
        Field[] declaredFields = developerClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            // 跳过检查，获取私有属性信息
            declaredField.setAccessible(true);
            System.out.println("declaredField.getName() = " + declaredField.getName());
            // 默认修饰符 是 0 ， public 是 1 ，private 是 2 ，protected 是 4 , static 是 8 ，final 是 16
            System.out.println("declaredField.getModifiers() = " + declaredField.getModifiers());
            System.out.println("declaredField = " + declaredField);
            System.out.println("declaredField.get(o) = " + declaredField.get(o));
            System.out.println("declaredField.getType() = " + declaredField.getType());

        }


        Method method = developerClass.getMethod("test");
        method.invoke(o);
        Method test = developerClass.getMethod("test", String.class);
        test.invoke(o,"message");

        // 静态方法
        Method testStatic = developerClass.getMethod("testStatic");
        testStatic.invoke(null);

        // 本类及父类的所有public 方法
        Method[] methods = developerClass.getMethods();
        for (Method method1 : methods) {
            System.out.println("method1 = " + method1);
            // 默认修饰符 是 0 ， public 是 1 ，private 是 2 ，protected 是 4 , static 是 8 ，final 是 16
            System.out.println("method1.getModifiers() = " + method1.getModifiers());
            System.out.println("method1.getName() = " + method1.getName());
            System.out.println("method1.getReturnType() = " + method1.getReturnType());
            System.out.println("method1.getParameterCount() = " + method1.getParameterCount());
            System.out.println("method1.getParameterTypes() = " + method1.getParameterTypes());

        }


        Class<?> superclass = developerClass.getSuperclass();
        System.out.println("superclass = " + superclass);


        Class<?>[] interfaces = developerClass.getInterfaces();


        for (Class<?> anInterface : interfaces) {
            System.out.println("anInterface = " + anInterface);
        }

        Type genericSuperclass = developerClass.getGenericSuperclass();
        System.out.println("genericSuperclass = " + genericSuperclass);


        Constructor<?> constructor = developerClass.getConstructor(String.class);

        Constructor<?> declaredConstructor = developerClass.getDeclaredConstructor(String.class, String.class);

        // 本类所有构造器
        Constructor<?>[] declaredConstructors = developerClass.getDeclaredConstructors();

        // 本类所有public 构造器
        Constructor<?>[] constructors = developerClass.getConstructors();

        for (Constructor<?> constructor1 : constructors) {
            System.out.println("constructor1 = " + constructor1);
            System.out.println("constructor1.getModifiers() = " + constructor1.getModifiers());
        }

        Annotation[] declaredAnnotations = developerClass.getDeclaredAnnotations();
        Annotation[] annotations = developerClass.getAnnotations();
//        developerClass.getAnnotation()


    }
}
